﻿//************************************************************************
//* Written by Jirat Boomuang
//* Filename: Program.cs
//* Revision: 1.0
//* Author: JiratB
//* Platform: .NET Framework 3.5
//* Description: Code for generating "Year" Option Sets which can be used
//*   for generating a solution and then "import" to MS Dynamics CRM
//* 
//************************************************************************

//************************************************************************
//* Revision History
//------------------------------------------------------------------------
//* Revision  Date      Signature    Description
//------------------------------------------------------------------------
//* 1.0       20130119  JiratB       - Released to Codeplex
//************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace YearGenerator
{
    class Program
    {
        static int startCalendarYear; // calendar year in AD
        static int endCalendarYear; // calendar year in AD

        static int startFinancialYear; // financial year in AD
        static int endFinancialYear; // financial year in AD

        const int BEminusAD = 543; 

        static void Main(string[] args)
        {
            Initialize(args);

            WriteHeader();
            WriteOptionSets();
            WriteLanguages();
            WriteFooter();

            //System.Console.ReadKey();
        }//-- End of void Main()

        static void Initialize(string[] args)
        {
            
            startCalendarYear = 1838;
            endCalendarYear = 2038;

            startFinancialYear = 1939;
            endFinancialYear = 2038;
            

            /*
            startCalendarYear = 1939;
            endCalendarYear = 1940;

            startFinancialYear = 1939;
            endFinancialYear = 1940;
             * */
        }//-- End of void Initialize() --

        static void WriteHeader()
        {
            //Console.WriteLine();
            Console.WriteLine("<ImportExportXml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
            WriteSpaces(2);
            Console.WriteLine("<Entities></Entities>");
            WriteSpaces(2);
            Console.WriteLine("<Roles></Roles>");
            WriteSpaces(2);
            Console.WriteLine("<Workflows></Workflows>");
            WriteSpaces(2);
            Console.WriteLine("<FieldSecurityProfiles></FieldSecurityProfiles>");
            WriteSpaces(2);
            Console.WriteLine("<Templates />");
            WriteSpaces(2);
            Console.WriteLine("<EntityMaps />");
            WriteSpaces(2);
            Console.WriteLine("<EntityRelationships />");
            WriteSpaces(2);
            Console.WriteLine("<OrganizationSettings />");
        }//-- End of WriteHeader() --

        static void WriteFooter()
        {
            WriteSpaces(0);
            Console.WriteLine("</ImportExportXml>");
        }//-- End of WriteFooter() --

        #region OptionSets
        static void WriteOptionSets()
        {
            WriteSpaces(2);
            Console.WriteLine("<optionsets>");

            WriteOptionSet_FinancialYear();
            WriteOptionSet_CalendarYearAD();
            WriteOptionSet_CalendarYearBE();

            WriteSpaces(2);
            Console.WriteLine("</optionsets>");
        }//-- End of WriteOptionSets() --

        static void WriteOptionSet_FinancialYear()
        {
            //-- Open Tag --
            WriteSpaces(4);
            Console.WriteLine(
                "<optionset Name=\"vxrm_financialyear\" localizedName=\"Financial Year\">"
                );

            #region Details
            //-- Write Details --
            WriteSpaces(6);
            Console.WriteLine("<OptionSetType>picklist</OptionSetType>");

            WriteSpaces(6);
            Console.WriteLine("<IsGlobal>1</IsGlobal>");

            WriteSpaces(6);
            Console.WriteLine("<IsCustomizable>1</IsCustomizable>");

            WriteSpaces(6);
            Console.WriteLine("<displaynames>");

            WriteSpaces(8);
            Console.WriteLine("<displayname description=\"Financial Year\" languagecode=\"1033\" />");

            WriteSpaces(6);
            Console.WriteLine("</displaynames>");

            WriteSpaces(6);
            Console.WriteLine("<Descriptions>");

            WriteSpaces(8);
            Console.WriteLine("<Description description=\"\" languagecode=\"1033\" />");

            WriteSpaces(6);
            Console.WriteLine("</Descriptions>");
            #endregion

            #region Loop
            WriteSpaces(6);
            Console.WriteLine("<options>");
            //-- Loop for each year --
            for (int year = startFinancialYear; year <= endFinancialYear; ++year)
            {
                int modYear = year % 100;

                #region Write Option and Value
                WriteSpaces(8);
                Console.WriteLine("<option value=\"" + AddPrefix(modYear) + "\">");

                #region Write Label
                WriteSpaces(10);
                Console.WriteLine("<labels>");

                WriteSpaces(12);
                if (modYear < 10)
                {
                    Console.WriteLine(
                        String.Format(
                        "<label description=\"FY0{0}\" languagecode=\"1033\" />",
                        modYear
                        )
                        );
                }
                else
                {
                    Console.WriteLine(
                        String.Format(
                        "<label description=\"FY{0}\" languagecode=\"1033\" />",
                        modYear
                        )
                        );
                }

                WriteSpaces(10);
                Console.WriteLine("</labels>");
                #endregion

                #region Write Description
                WriteSpaces(10);
                Console.WriteLine("<Descriptions>");

                WriteSpaces(12);
                Console.WriteLine(
                    String.Format(
                    "<Description description=\"{0}\" languagecode=\"1033\" />",
                    year
                    )
                    );

                WriteSpaces(10);
                Console.WriteLine("</Descriptions>");
                #endregion

                WriteSpaces(8);
                Console.WriteLine("</option>");
                #endregion
            }
            //-- End of loop --
            WriteSpaces(6);
            Console.WriteLine("</options>");
            #endregion

            //-- Close Tag --
            WriteSpaces(4);
            Console.WriteLine("</optionset>");
        }//-- End of WriteOptionSet_FinancialYear() --

        static void WriteOptionSet_CalendarYearAD()
        {
            //-- Open Tag --
            WriteSpaces(4);
            Console.WriteLine(
                "<optionset Name=\"vxrm_yearadlist\" localizedName=\"Year (AD) List\" description=\"List of year in AD; added by JiratB, 20121216\">"
                );

            #region Details
            //-- Write Details --
            WriteSpaces(6);
            Console.WriteLine("<OptionSetType>picklist</OptionSetType>");

            WriteSpaces(6);
            Console.WriteLine("<IsGlobal>1</IsGlobal>");

            WriteSpaces(6);
            Console.WriteLine("<IsCustomizable>1</IsCustomizable>");

            WriteSpaces(6);
            Console.WriteLine("<displaynames>");

            WriteSpaces(8);
            Console.WriteLine("<displayname description=\"Year (AD) List\" languagecode=\"1033\" />");

            WriteSpaces(6);
            Console.WriteLine("</displaynames>");

            WriteSpaces(6);
            Console.WriteLine("<Descriptions>");

            WriteSpaces(8);
            Console.WriteLine("<Description description=\"List of year in AD; added by JiratB, 20121216\" languagecode=\"1033\" />");

            WriteSpaces(6);
            Console.WriteLine("</Descriptions>");
            #endregion

            #region Loop
            WriteSpaces(6);
            Console.WriteLine("<options>");
            //-- Loop for each year --
            for (int year = startCalendarYear; year <= endCalendarYear; ++year)
            {
                #region Write Option and Value
                WriteSpaces(8);
                Console.WriteLine("<option value=\"" + AddPrefix(year) + "\">");

                #region Write Label
                WriteSpaces(10);
                Console.WriteLine("<labels>");

                WriteSpaces(12);
                Console.WriteLine(
                    String.Format(
                    "<label description=\"{0}\" languagecode=\"1033\" />",
                    year
                    )
                    );
                
                WriteSpaces(10);
                Console.WriteLine("</labels>");
                #endregion

                #region Write Description
                WriteSpaces(10);
                Console.WriteLine("<Descriptions>");

                WriteSpaces(12);
                Console.WriteLine(
                    String.Format(
                    "<Description description=\"{0}\" languagecode=\"1033\" />",
                    year
                    )
                    );

                WriteSpaces(10);
                Console.WriteLine("</Descriptions>");
                #endregion

                WriteSpaces(8);
                Console.WriteLine("</option>");
                #endregion
            }
            //-- End of loop --
            WriteSpaces(6);
            Console.WriteLine("</options>");
            #endregion

            //-- Close Tag --
            WriteSpaces(4);
            Console.WriteLine("</optionset>");
        }//-- End of WriteOptionSet_CalendarYearAD() --

        static void WriteOptionSet_CalendarYearBE()
        {
            //-- Open Tag --
            WriteSpaces(4);
            Console.WriteLine(
                "<optionset Name=\"vxrm_yearbelist\" localizedName=\"Year (BE) List\" description=\"List of year in BE; added by JiratB, 20121216\">"
                );

            #region Details
            //-- Write Details --
            WriteSpaces(6);
            Console.WriteLine("<OptionSetType>picklist</OptionSetType>");

            WriteSpaces(6);
            Console.WriteLine("<IsGlobal>1</IsGlobal>");

            WriteSpaces(6);
            Console.WriteLine("<IsCustomizable>1</IsCustomizable>");

            WriteSpaces(6);
            Console.WriteLine("<displaynames>");

            WriteSpaces(8);
            Console.WriteLine("<displayname description=\"Year (BE) List\" languagecode=\"1033\" />");

            WriteSpaces(6);
            Console.WriteLine("</displaynames>");

            WriteSpaces(6);
            Console.WriteLine("<Descriptions>");

            WriteSpaces(8);
            Console.WriteLine("<Description description=\"List of year in BE; added by JiratB, 20121216\" languagecode=\"1033\" />");

            WriteSpaces(6);
            Console.WriteLine("</Descriptions>");
            #endregion

            #region Loop
            WriteSpaces(6);
            Console.WriteLine("<options>");
            //-- Loop for each year --
            for (int year = startCalendarYear; year <= endCalendarYear; ++year)
            {
                #region Write Option and Value
                WriteSpaces(8);
                Console.WriteLine("<option value=\"" + AddPrefix(year+BEminusAD) + "\">");

                #region Write Label
                WriteSpaces(10);
                Console.WriteLine("<labels>");

                WriteSpaces(12);
                Console.WriteLine(
                    String.Format(
                    "<label description=\"{0}\" languagecode=\"1033\" />",
                    year+BEminusAD
                    )
                    );

                WriteSpaces(10);
                Console.WriteLine("</labels>");
                #endregion

                #region Write Description
                WriteSpaces(10);
                Console.WriteLine("<Descriptions>");

                WriteSpaces(12);
                Console.WriteLine(
                    String.Format(
                    "<Description description=\"{1}BE = {0}AD\" languagecode=\"1033\" />",
                    year, year+BEminusAD
                    )
                    );

                WriteSpaces(10);
                Console.WriteLine("</Descriptions>");
                #endregion

                WriteSpaces(8);
                Console.WriteLine("</option>");
                #endregion
            }
            //-- End of loop --
            WriteSpaces(6);
            Console.WriteLine("</options>");
            #endregion

            //-- Close Tag --
            WriteSpaces(4);
            Console.WriteLine("</optionset>");
        }//-- End of WriteOptionSet_CalendarYearBE() --
        #endregion

        #region Languages
        static void WriteLanguages()
        {
            WriteSpaces(2);
            Console.WriteLine("<Languages>");

            WriteLanguageCode(LanguageCode_English());
            WriteLanguageCode(LanguageCode_Thai());

            WriteSpaces(2);
            Console.WriteLine("</Languages>");
        }//-- End of WriteLanguages() --

        static void WriteLanguageCode(int languageCode)
        {
            WriteSpaces(4);
            Console.WriteLine(
                String.Format(
                "<Language>{0}</Language>",
                languageCode
                )
                );
        }//-- End of WriteLanguageCode() --

        static int LanguageCode_English()
        {
            return 1033;
        }//-- End of LanguageCode_English() --

        static int LanguageCode_Thai()
        {
            return 1054;
        }//-- End of LanguageCode_Thai() --
        #endregion

        static void WriteSpaces(int numberOfSpace)
        {
            for (int loop = 0; loop < numberOfSpace; ++loop)
                Console.Write(" ");
        }//-- End of WriteSpaces() --

        static string AddPrefix(int value)
        {
            return String.Format(
                "{0}",
                100000000+value
                );
        } //-- End of AddPrefix() --
    }//-- End of class Program --
}

//************************************************************************
// End of file
//************************************************************************